'use strict';

import Constant from './constant';

export default {
    // mock request function
    getExample(params) {
        params.path = '/api/example';
        request(params);
    },
    getList(params) {
        params.path = '/list/' + params.reqParams.type;
        request(params);
    },
    getUnread(params){
        params.path = '/unread/'  + params.reqParams.type;
        request(params);
    },
    getAppList(params) {
        params.path = '/appList/' + params.reqParams.type;
        // params.path = '/src/applist.json';
        request(params);
    }
};

function request(params) {
    var defaults = {
        // url: Constant.SERVER_URL + "/demo" + params.path,
        url: Constant.SERVER_URL + params.path,
        type: 'GET',
        contentType: 'application/json',
        dataType: 'json',
        complete: function(request, status) {}
    }
    $.each(defaults, function(key, val){
        if (!params[key]) {
            params[key] = val;
        }
    });
    
    var _successFn = params.success;
    params.success = function(result, status, xhr){
        if (false) {
            //拦截
        }
        _successFn(result, status, xhr);
    };
    
    if (params.type.toUpperCase() === 'POST' && params.contentType && params.contentType.indexOf('json') != -1) {
        params.data = JSON.stringify(params.data);
    }
    console.log('调用接口:\n%s,\n参数列表:', params.url, params.data);
    $.ajax(params);
};